package com.wcgksoft.controller.openAccount;

import com.sun.istack.NotNull;
import com.wcgksoft.common.util.Layui;
import com.wcgksoft.entity.system.*;
import com.wcgksoft.service.EnterpriseProjectService;
import com.wcgksoft.service.SysRoleCodeService;
import com.wcgksoft.service.openAccount.OpenAccountService;
import com.wcgksoft.service.system.AuthorityService;
import com.wcgksoft.service.system.EnterpriseService;
import com.wcgksoft.service.system.SysRoleService;
import javafx.scene.chart.ValueAxis;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashSet;
import java.util.List;

/**
 * @ClassName OpenAccountController
 * @Description 开设管理账户
 * @Author user
 * @Date 2020/3/9 13:41
 * @Version V1.0
 **/
@RequestMapping("system/openAccount")
@Controller
public class OpenAccountController {

    @Autowired
    private OpenAccountService openAccountService;

    @Autowired
    private AuthorityService authorityService;


    /**
     * 查询权限类型
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "findAuthority")
    public Layui findAuthorType(){
        DetachedCriteria criteria = DetachedCriteria.forClass(Authority.class);
        List<Authority> list = authorityService.findByConditions(criteria);
        return Layui.data(0, "success", list);
    }


    /**
     * 开设账号 不同权限
     * */
    @ResponseBody
    @RequestMapping(value = "openAccount")
    public Layui openAccount(HttpServletRequest request,
                             @RequestParam(value = "data", required = false)String data)
    {
        JSONObject param = JSONObject.fromObject(data);
        if (param == null)
        {
            return Layui.data(1, "请求格式有误", 0, null);
        } else {
            SysUser sysUser = (SysUser) JSONObject.toBean(JSONObject.fromObject(data), SysUser.class);
            String id=(String)param.get("id");
            if (StringUtils.isNotEmpty(id)){
                String aId = (String) param.get("authorId");
                Authority authority = authorityService.findById(Integer.parseInt(aId));
                sysUser.setAuthority(authority);
                sysUser.setStatus(1);
                openAccountService.saveMerge(sysUser);
            }else {
                String aId = (String) param.get("authorId");
                Authority authority = authorityService.findById(Integer.parseInt(aId));
                sysUser.setAuthority(authority);
                sysUser.setStatus(1);
                sysUser.setCreateDate(new Date());
                openAccountService.save(sysUser);
            }
            return Layui.data(0, "success",1, Layui.addList(sysUser));
        }
    }

    /**
     * 查询全部用户信息 或者根据name查询
     * @param page
     * @param limit
     * @param name
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "findAll")
    public Layui findAll(@RequestParam(value = "page", required = false) Integer page,
                         @RequestParam(value = "limit", required = false) Integer limit,
                         @RequestParam(value = "keyWord",required = false)String name)
    {
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getSession().getAttribute("om_user");
        DetachedCriteria criteria = DetachedCriteria.forClass(SysUser.class);
        if(StringUtils.isNotEmpty(name)) {
            criteria.add(Restrictions.like("name", name, MatchMode.ANYWHERE));
        }
        List<SysUser> list = openAccountService.findByConditions(criteria, page, limit);
        return Layui.data(0, "查询成功", openAccountService.getTotalCount(), list);
    }

    @ResponseBody
    @RequestMapping(value = "deleteById")
    @Transactional
    public Layui deleteById(@RequestParam(value = "pid",required = false)Integer id)
    {
        SysUser sysUser=null;
        try{
            sysUser=openAccountService.findById(id);
            openAccountService.deleteCass(sysUser);
        }catch (Exception e){

        }
        return Layui.data(0, "删除成功", 1,null);
    }






}
